package us.codecraft.webmagic.httpClientUtil;


import org.apache.http.config.Registry;
import org.apache.http.config.RegistryBuilder;
import org.apache.http.conn.socket.ConnectionSocketFactory;
import org.apache.http.conn.socket.LayeredConnectionSocketFactory;
import org.apache.http.conn.socket.PlainConnectionSocketFactory;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;

public class HttpClientPool {

    /**
     *
     * @param maxPoolNum httpClient连接池最大连接数
     * @param defaultRouteNum 默认路由的连接数，即连接每个网站的最大数量。
     * @return
     */
    public static PoolingHttpClientConnectionManager getHttpConnectionPool(int maxPoolNum, int defaultRouteNum){
        ConnectionSocketFactory plainsf  =  PlainConnectionSocketFactory.getSocketFactory();
        LayeredConnectionSocketFactory sslsf  =  SSLConnectionSocketFactory.getSocketFactory();
        Registry<ConnectionSocketFactory> registry  =  RegistryBuilder.<ConnectionSocketFactory>create()
                .register("http",  plainsf)
                .register("https",  sslsf)
                .build();
        PoolingHttpClientConnectionManager cm  =  new PoolingHttpClientConnectionManager(registry);

        cm.setMaxTotal(maxPoolNum);

        cm.setDefaultMaxPerRoute(defaultRouteNum);

        return cm;
    }
}
